package com.mccken.rapid.core;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

import com.esotericsoftware.minlog.Log;
import com.mccken.rapid.common.util.PropertiesUtils;

/**
 * @program: rapid_gateway
 * @description: 网关配置信息加载类
 * 网关配置加载规则，优先级顺序如下(高的优先级会覆盖掉低的优先级)：
 *  运行参数(最高) -> jvm参数 -> 环境变量 -> 配置文件 -> 内部config对象的默认属性值(最低)
 * 
 * @author: mccken
 * @create: 2022-01-03 18:07
 **/
public class RapidConfigLoader {
	
	public static final String CONFIG_ENV_PREFIEX = "RAPID_";
	public static final String CONFIG_JVM_PREFIEX = "rapid.";
	
	public static final String CONFIG_FILE = "rapid.properties";
	
	private final static RapidConfigLoader INSTANCE = new RapidConfigLoader();
	
	private RapidConfig rapidConfig = new RapidConfig();
	
	private RapidConfigLoader(){
		
	}
	
	public static RapidConfigLoader getInstance() {
		return INSTANCE; 
	}
	
	
	
	public static RapidConfig getRapidConfig() {
		return INSTANCE.rapidConfig;
	}
	
	public RapidConfig load(String[] args) {
		
		// 加载逻辑
		
		// 1. 配置文件
		{
			InputStream is = RapidConfigLoader.class.getClassLoader().getResourceAsStream(CONFIG_FILE);
			if (is != null) {
				Properties properties = new Properties();
				try {
					properties.load(is);
					PropertiesUtils.properties2Object(properties,rapidConfig);
				} catch (IOException e) {
					Log.warn("#RapidConfigLoader# load config file:{} is error", CONFIG_FILE, e);
				} finally {
					if (is != null) {
						try {
							is.close();
						} catch (IOException e) {
							// ignore
						}
					}
				}
			}
		}
		
		// 2. 环境变量
		{
			Map<String, String> env = System.getenv();
			Properties properties = new Properties();
			properties.putAll(env);
			PropertiesUtils.properties2Object(properties, rapidConfig, CONFIG_ENV_PREFIEX);
		}
		
		
		// 3. JVM参数
		{
			Properties properties = System.getProperties();
			PropertiesUtils.properties2Object(properties, rapidConfig, CONFIG_JVM_PREFIEX);
		}
		
		// 4. 运行参数：--xxx=xxx --enable=true --port=1234
		{
			if (args != null && args.length > 0) {
				Properties properties = new Properties();
				for (String arg : args) {
					if (arg.startsWith("--") && arg.contains("=")) {
						properties.put(arg.substring(2, arg.indexOf("=")), arg.substring(arg.indexOf("=") + 1));
					}
				}
				PropertiesUtils.properties2Object(properties, rapidConfig);
			}
		}
		
		
		return rapidConfig;
	}
	
}
